package uk.ac.glasgow.demosocs.tests;

import static org.junit.Assert.*;

import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uk.ac.glasgow.demosocs.ElectionResult;
import uk.ac.glasgow.demosocs.OneOfNVote;
import uk.ac.glasgow.demosocs.VotingSystem;
import uk.ac.glasgow.demosocs.impl.OneOfNVoteStub;
import uk.ac.glasgow.demosocs.impl.VotingSystemStub;

public class NFVotes {

	private VotingSystem testVS;
	private OneOfNVote testV;
	
	@Before
	public void setUp() throws Exception {
		testVS = new VotingSystemStub();

		testVS.setCurrentUser("admin", "p");
		testVS.createElection("e1", "Simple_Plurality");
	}

	@After
	public void tearDown() throws Exception {
		testVS = null;
		testV = null;
	}

	@Test
	/** 
	 * tests castVote - sets up an election, nominates two candidates,
	 * votes for one of them, checks winner is one voted for
	 * 
	 */
	public void test() {
		
		String c1 = "1";
		String c2 = "2";
		
		String eid = testVS.getCurrentElection().getEID();

		
		Date startD = new Date();
		startD.setTime(startD.getTime() + 1000000000000000000L);
		testVS.editElection(new Date(), startD, startD, "multipleTest");
		//fails here with invalid electorate type, 
		//unless comments removed in set up section in VotingSystemStub
		
		testVS.setCurrentElection(eid);
		
		testVS.nominateCandidate(c1);
		testVS.setCurrentUser(c1, "p");
		testVS.acceptNomination();
		testVS.nominateCandidate(c2);		
		testVS.setCurrentUser(c2, "p");		
		testVS.acceptNomination();
		
		Date endD = new Date();
		endD.setTime(endD.getTime() + 1000000000000000000L);
		
		testVS.editElection(new Date(), new Date(), endD, eid);
		testVS.setCurrentElection(eid);

		
		for (int i = 5; i < 100006; i++) {
			testVS.setCurrentUser(Integer.toString(i), "p");
			testV = new OneOfNVoteStub(c1);
			testVS.castVote(testV);
		}
			
		testVS.editElection(new Date(), new Date(), new Date(), eid);
		
		testVS.publishResults();
		ElectionResult r = testVS.getCurrentElection().getElectionResult();
				
		String cwin = r.getWinningOptions().get(0).getDisplayableDescription();	//getWinningCandidates()
		
		assertEquals(c1, cwin);
		
	}

}
